<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-input v-model="form.id" type="hidden"></el-input>
      <el-form-item label="确认时间" prop="birthday">
        <el-input v-model="form.birthday" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="报警原因" prop="suremsg">
        <el-input
          v-model="form.suremsg"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6 }"
          placeholder="请输入报警原因描述"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { saveAlarmReason } from '@/api/alarm'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        form: {
          id: '',
          suremsg: '',
          birthday: '',
        },
        rules: {
          own_nickname: [
            { required: 1, message: '请输入用户姓名', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          accname: [
            { required: 1, message: '请输入账号', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          mobileno: [
            { required: 1, message: '请输入手机号', trigger: 'blur' },
            {
              min: 11,
              max: 13,
              message: '长度在 11 到 13 个字符',
              trigger: 'blur',
            },
          ],
          email: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            {
              type: 'email',
              message: '请输入正确的邮箱地址',
              trigger: ['blur', 'change'],
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.birthday = new Date().toLocaleDateString()
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveAlarmReason({
              id: this.form.id,
              suremsg: this.form.suremsg,
            })
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
